<template>
  <div class="wrapper">
     <!-- 回到顶部 -->
      <el-backtop target=".wrapper">
        <i class="el-icon-caret-top"></i>
      </el-backtop>
      
      <!-- 头部导航 -->
      <Head aIndex="1"></Head>

      <!-- 中间内容 -->
      <div style="margin-top: 50px; min-height:80vh;">
        <el-row :gutter="5">

          <!-- 空白占位 -->
           <el-col :span="3">
             <div class="grid-content bg-purple">
               <div></div>
             </div>
           </el-col>

          <!-- 左侧 -->
          <el-col :span="5" :offset="1">
            <div class="grid-content bg-purple " >
              <!-- 简介 -->
              <el-card class="box-card" >
                <div style="text-align: center;" v-lazy-container="{ selector: 'img' }">
<!--                  <img width="100" height="100" v-lazy="../../assets/image/head.jpg" />-->
                  <img width="100" height="100" data-src="http://photogz.photo.store.qq.com/psc?/V518IhTc1ak1tt3yl2dJ2vkXwd4UwPld/45NBuzDIW489QBoVep5mcfMu*HmmR0w5v7KAt.3ECPrwZwtnN9caZpYGizKb*7wFdm3Q86TlYHiXGHwWhTFbp0OLUfQNEcGVF3JwQNNCf68!/b&bo=OASOBDgEjgQBGT4!&rf=viewer_4&t=5" />
                  <p style="line-height: 0;font-size: 20px;">Jiang_JY</p>
                  <p style="font-size: 15px;">欢迎来到我的博客.</p>
                </div>
              </el-card>

              <!-- 分类专栏 -->
              <el-card class="box-card" style="margin-top: 20px;">
                <div slot="header" class="clearfix">
                  <span>分类专栏</span>
                  <router-link to="/blogType">
                    <el-button style="float: right; padding: 3px 0" type="text">more <i class="el-icon-d-arrow-right"></i></el-button>
                  </router-link>
                </div>
                <div class="text item" v-for="(item,indexTypes) in types" :key="indexTypes" >
                      <span style="font-size: 14px;"  >
<!--                        @click="getType(item.id)"-->
                          <el-card v-if="item.id > 0" shadow="hover" style="margin-bottom: 5px;">
                            <el-link :underline="false">
                               {{item.name }}
                            </el-link>
                            <span style="float: right;"> {{item.countType}}</span>
                          </el-card>
                      </span>
                  <!-- <div v-if="index!=4" style="background-color: #DCDFE6;height: 1px;margin: 5px 0;"></div> -->
                </div>
              </el-card>

              <!-- 热门文章 -->
              <el-card class="box-card" style="margin-top: 20px;">
                <div slot="header" class="clearfix">
                  <span>热门文章</span>
                  <el-button style="float:right; padding: 3px 0" type="text">Top5</el-button>
                </div>
                <div class="text item" v-for="(item,index) in HotBlog" :key="index" >
                  <router-link :to="'/blog/'+ item.id">
                    <el-link :underline="false">
                      <span style="font-size: 14px;">
                        <el-tag size="small">{{index + 1}}</el-tag> {{item.title}}
                        <i style="margin-left: 5px;" class="el-icon-view"><span style="font-size: 13px;"> {{item.views}}</span></i>
                      </span>
                    </el-link>
                  </router-link>
                  <div v-if="index!=4" style="background-color: #DCDFE6;height: 1px;margin: 12px 0;"></div>
                </div>
              </el-card>
            </div>
          </el-col>

          <!-- 右侧 -->
          <el-col :span="16" :offset="1" :style="{bgColor:'#f9f9f9'}">
            <div class="grid-content bg-purple blog_bg_left" v-loading="loading">

              <el-row :gutter="20">
                <el-col :span="12"><div style="margin: 15px 0px 15px 20px;color: blueviolet;font-size: 20px;">文章</div></el-col>
                <el-col :span="12"><div style="float: right;margin: 10px 20px 0px 0px;font-size:20px;">共 <span style="font-size: 28px;color: rgb(6, 154, 240);">{{total}}</span> 篇</div></el-col>
              </el-row>

              <!-- 分割线 -->
              <div class="fenge"></div>
              <!-- <el-divider></el-divider> -->

              <!-- 文章 -->
              <div>
                <div class="blog_card" v-for="(item,index) in blogData" :key="index" >

<!--                 <router-link :to="'/blog/'+item.id">-->
                <div @click="goToBlog(item.id)">
                  <el-card shadow="hover" :body-style="{ padding: '15px' }" >
                    <el-row :gutter="20">
                      <el-col :span="17">
                        <div>                        
                           <div style="font-size: 26px;">{{item.title}}</div>
                           <p style="font-size: 17px;">{{item.description}}</p>
                           <div v-lazy-container="{ selector: 'img' }">
<!--                            <el-avatar style="vertical-align:middle" :src="item.tuser.avatar"></el-avatar>-->
                              <img
                                 :data-src="item.tuser.avatar"
                                 style="width:45px;height:45px;border-radius: 50%;vertical-align:middle"
                              />
                              <span style="margin-left: 5px;">admin</span>
                              <i style="margin-left: 20px;" class="el-icon-date"><span style="font-size: 18px;"> {{item.createTime}}</span></i>
                              <i style="margin-left: 20px;" class="el-icon-view"><span style="font-size: 18px;"> {{item.views}}</span></i>
                              <i style="margin-left: 20px;" class="el-icon-chat-line-square"><span style="font-size: 18px;"> {{item.commentCount}}</span></i>
                           </div>

                        </div>
                      </el-col>
                      <el-col :span="7">
                        <div v-lazy-container="{ selector: 'img' }">
<!--                          <el-image-->
<!--                            style="width: 200px; height: 115px"-->
<!--                            :src="item.firstPicture"-->
<!--                            >-->
<!--                          </el-image>-->
                          <img
                              style="width: 200px; height: 115px"
                              :data-src="item.firstPicture"
                          />
                          <div style="margin-bottom: 10px;">
                            <el-tag style="margin-right: 10px;" ><span> {{item.flag}}</span></el-tag>
                            <el-tag style="margin-right: 10px;" type="success"><span > {{item.ttype.name}}</span></el-tag>
                            <!-- <el-tag style="margin-right: 10px;" type="info" v-for="tag in item.tblogTags"><span> {{tag.ttag.name}}</span></el-tag> -->
                          </div>
                        </div>
                      </el-col>
                    </el-row>
                    
                  </el-card>
                   </div>
<!--                 </router-link>-->

                  <el-divider></el-divider>      

                 </div>
              </div>

              <div style="margin-left: 2%;padding-bottom: 2%;">
                  <!-- 分页 -->
                  <el-pagination
                  background
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  :current-page.sync="currentPage"
                  :page-size="size"
                  layout="total, prev, pager, next"
                  :total="total">
                  </el-pagination>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>

      <!-- <el-backtop target=".page-component__scroll .el-scrollbar__wrap"></el-backtop> -->

      <Foot class="foot"></Foot>

  </div>
</template>

<script>
import Head from '@/components/Head'
import Foot from '@/components/Foot'

export default {
  name: 'Home',
  components:{ Head,Foot },
  data () {
   return {
     blogData:[],
     HotBlog:[], //热门文章
     types: [],
     total:0,
     currentPage:1,
     size:5,
     aIndex: '1',
     source:"blog",
     key:0,
     loading:true,//文章的加载
     hotBlogLoading: true,//热门文章的加载
     typeLoading: true,//标签加载
   }
  },
  created() {
    this.initBlog()   //页面加载时调用
    this.initHotBlog()   //页面加载时调用
    this.initAllType()   //页面加载时调用
  },

  mounted() {
    this.key = this.$store.getters.getHomeVal
  },

  updated(){
    // console.log("执行了")
    var _this = this
    this.changeblogCountFun(_this.total)
  },

  methods:{

    // 初始化博客列表数据
    initBlog(){
      const _this = this

      // console.log(this.currentPage,this.size)
      this.currentPage = this.$store.getters.getHomeVal
      this.axios.get('/blog/vuefindByPage?current=' + this.currentPage + '&size=' + this.size + '&source=' + this.source).then(resp=>{
        // console.log("初始博客列表数据")
        // console.log(resp)
        _this.blogData = resp.data.obj.blogList
        _this.total = resp.data.obj.totals
        this.changeblogCountFun(resp.data.obj.totals)
        this.loading = false
      })
    },

    //给store中的文章数传值
    changeblogCountFun(total){
      this.$store.dispatch("changeblogCountFun",total)
    },

    //初始化热门文章
    initHotBlog(){
      const _this = this
      this.axios.get('/blog/vuefindHotBlog').then(resp=>{
        // console.log("初始化热门文章")
        // console.log(resp)
        _this.HotBlog = resp.data.obj
        this.hotBlogLoading = false
      })
    },
    //查询所有分类
    initAllType(){
      const _this = this
      this.axios.get('/type/getAllType').then(resp=>{
        // console.log(resp)
        // _this.types = resp.data.obj
        for(var i=0;i<5;i++){
          _this.types.push(resp.data.obj[i])
        }
        this.typeLoading = false
      })
    },

    // // 获取分类数据
    // getType(id){
    //   // console.log(id)
    //   this.type_id = id
    //   this.source = 'type'
    //   this.initBlogType()
    // },

    // //初始化博客数据
    // initBlogType(){
    //   const _this = this
    //   this.currentPage = this.$store.getters.getHomeVal
    //   this.axios.get('/blog/vuefindByPage?current=' + this.currentPage + '&size=' + this.size + '&type_id=' + this.type_id + '&source=' + this.source).then(resp=>{
    //     // console.log(resp)
    //     _this.blogData = resp.data.obj.blogList
    //     _this.total = resp.data.obj.blogList.length
    //   })
    // },

    goToBlog(id){
      // console.log(id)
      this.$router.push('/blog/'+id)
      // this.$router.replace({path:'/blog/'+id})
      this.changeSourceFun("blog")
    },

    // 翻页后滚动条回到页面顶端
    scrollTop(selector) {
      let element = selector && document.querySelector(selector) || window;
      element.scrollTo(0, 0);
    },

    //分页
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.loading = true
      const _this = this
      this.axios.get('/blog/vuefindByPage?current=' + val + '&size=' + this.size + '&source=' + this.source).then(resp=>{
        // console.log(resp)
        this.loading = false
        _this.blogData = resp.data.obj.blogList
        _this.total = resp.data.obj.totals

        //将blogData存进vuex中
        this.changeHomeBlogFun(resp.data.obj.blogList)
        //找到你想要滚到顶部的元素scrollTop=0
        this.scrollTop(".wrapper");
        this.changeHomeValFun(val)
      })
    },

    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
    },

    //  改变store中homeVal的值
    changeHomeValFun(val){
      this.$store.dispatch("changeHomeValFun",val)
    },

    // 改变store中source的值
    changeSourceFun(source){
      this.$store.dispatch("changeSourceFun",source)
    },

    // //改变store中homeBlog的值
    // changeHomeBlogFun(source){
    //   this.$store.dispatch("changeHomeBlogFun",source)
    // },

  }
}
</script>

<style scoped>
  .wrapper{
    height: 100vh;
    overflow-x: hidden;
    /*position: relative;*/
  }

  /* 卡片 */
  .blog_card{
    width: 95%;
    margin: 2%;
  }
  /* logo */
  .logo{
    margin-left: 150px;
    font-size: 30px;
    font-family: 华文行楷;
    color: #409eff !important; 
  }
  /* 左侧 */
  .blog_bg_left{
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    width: 100%;
    min-height: 500px;
  }
  /* 右侧 */
  .blog_bg_right{
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    width: 100%;
    min-height: 500px;
  }
  /* 分割线 */
  .fenge{
    height:3px;
    border:none;
    border-top:3px solid  #1eb9e0;
  }

  .foot{
    margin-top: 80px;
  }
</style>